package top.zackyoung.bbs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import top.zackyoung.bbs.entity.*;
import top.zackyoung.bbs.service.UserService;
import top.zackyoung.bbs.utils.PageViewUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: ZackYoung
 * @time: 2020/6/8
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 我的主页
     * @param mav
     * @return
     */

    @RequestMapping("/user/control/home")
    public ModelAndView homePage(ModelAndView mav) {
        mav.setViewName("home");
        mav.addObject("baseURI", "/user/control/home");
        return mav;
    }

    /**
     * 个人修改
     * @param mav
     * @param allowUserDynamic
     * @param oldPassword
     * @param password
     * @param nickname
     * @param req
     * @return
     */

    @RequestMapping("/user/control/editUser")
    public ModelAndView editUserPage(ModelAndView mav, String allowUserDynamic, String oldPassword, String password, String nickname, HttpServletRequest req) {
        mav.setViewName("editUser");
        mav.addObject("baseURI", "/user/control/editUser");
        if (allowUserDynamic != null || oldPassword != null || password != null || nickname != null) {
            User user = (User) req.getSession().getAttribute("userList");
            String reslut = userService.updateUser(user, password, oldPassword, nickname, allowUserDynamic);
            mav.addObject("result", reslut);
        }

        return mav;
    }

    /**
     * 登录日志
     * @param mav
     * @param req
     * @param page
     * @return
     */

    @RequestMapping("/user/control/userLoginLogList")
    public ModelAndView loginLog(ModelAndView mav, HttpServletRequest req, Integer page) {
        mav.setViewName("userLoginLogList");
        User user = (User) req.getSession().getAttribute("userList");
        Integer id = user.getId();
        int totalRecord = userService.queryUserLoginLogCount(id);
        int limitPage = 30;
        if (page == null) {
            page = 1;
        }
        PageViewUtil pageViewUtil = new PageViewUtil();
        PageView pageView = pageViewUtil.pageView(page, totalRecord, "/user/control/userLoginLogList?page=", limitPage);

        List<Log> logList = userService.queryUserLoginLog(id, (pageView.getCurrentpage() - 1) * limitPage, limitPage);
        mav.addObject("logList", logList);
        mav.addObject("pageView", pageView);
        mav.addObject("baseURI", "/user/control/userLoginLogList");
        return mav;
    }

    /**
     * 关注页
     * @param mav
     * @param req
     * @param page
     * @return
     */
    @RequestMapping("/user/control/focus")
    public ModelAndView focusPage(ModelAndView mav, HttpServletRequest req, Integer page) {
        mav.setViewName("focus");
        Integer id = userList(req).getId();
        List<Focus> focusTotal = userService.queryFocusObject(id, null,null,true);
        int totalRecord = focusTotal.get(0).getTotal();
        int limitPage = 30;
        PageView pageView = pageCommon(totalRecord,30,page,"/user/control/focus?page=");
        List<Focus> resultList = userService.queryFocusObject(id, (pageView.getCurrentpage() - 1) * limitPage, limitPage,false);
        mav.addObject("resultList", resultList);
        mav.addObject("pageView", pageView);
        mav.addObject("baseURI", "/user/control/focus");
        mav.addObject("total",totalRecord);
        return mav;
    }
    @RequestMapping("/user/control/commentList")
    public ModelAndView commentListPage(ModelAndView mav, HttpServletRequest req, Integer page) {
        mav.setViewName("commentList");
        Integer id = userList(req).getId();
        List<Comment> commentTotal = userService.queryMyComment(id, null,null,true);
        int totalRecord = commentTotal.get(0).getTotal();
        int limitPage = 30;
        PageView pageView = pageCommon(totalRecord,30,page,"/user/control/commentList?page=");
        List<Comment> resultList = userService.queryMyComment(id, (pageView.getCurrentpage() - 1) * limitPage, limitPage,false);
        mav.addObject("resultList", resultList);
        mav.addObject("pageView", pageView);
        mav.addObject("baseURI", "/user/control/commentList");
        mav.addObject("total",totalRecord);
        return mav;
    }
    @RequestMapping("/user/control/commentReply")
    public ModelAndView commentReplyPage(ModelAndView mav, HttpServletRequest req, Integer page) {
        mav.setViewName("commentReply");
        Integer id = userList(req).getId();
        List<Comment> commentTotal = userService.queryMyCommentReply(id, null,null,true);
        int totalRecord = commentTotal.get(0).getTotal();
        int limitPage = 30;
        PageView pageView = pageCommon(totalRecord,30,page,"/user/control/commentReply?page=");
        List<Comment> resultList = userService.queryMyCommentReply(id, (pageView.getCurrentpage() - 1) * limitPage, limitPage,false);
        mav.addObject("resultList", resultList);
        mav.addObject("pageView", pageView);
        mav.addObject("baseURI", "/user/control/commentReply");
        mav.addObject("total",totalRecord);
        return mav;
    }
    @RequestMapping("/user/control/likeList")
    public ModelAndView likeListPage(ModelAndView mav, HttpServletRequest req, Integer page) {
        mav.setViewName("likeList");
        Integer id = userList(req).getId();
        List<Like> likeTotal = userService.queryMyLike(id, null,null,true);
        int totalRecord = likeTotal.get(0).getTotal();
        int limitPage = 30;
        PageView pageView = pageCommon(totalRecord,30,page,"/user/control/likeList?page=");
        List<Like> resultList = userService.queryMyLike(id, (pageView.getCurrentpage() - 1) * limitPage, limitPage,false);
        mav.addObject("resultList", resultList);
        mav.addObject("pageView", pageView);
        mav.addObject("baseURI", "/user/control/likeList");
        mav.addObject("total",totalRecord);
        return mav;
    }



    PageView pageCommon(int totalRecord,int limitPage,Integer page,String url){
        if (page == null) {
            page = 1;
        }
        PageViewUtil pageViewUtil = new PageViewUtil();
        PageView pageView = pageViewUtil.pageView(page, totalRecord, url, limitPage);

        return pageView;
    }
    User userList(HttpServletRequest req){
        User user = (User) req.getSession().getAttribute("userList");
        return user;
    }



}
